<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>人员新增</title>
    <script src="../../../../../res/js/jquery-3.4.1.min.js"></script>
    <script charset="utf-8" src="../../../../../res/layui/layui.js"></script>
    <script src="../../../../../res/js/Tools.js"></script>
    <link href="../../../../../res/layui/css/layui.css" media="all" rel="stylesheet">
    <link href="../../../../../res/echarts/eleTree.css" rel="stylesheet"/>
    <script src="../../../../../res/echarts/eleTree.js"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../../res/images/favicon.ico">
    <script charset="utf-8" src="../../../../../res/js/login_common.js"></script>
    <style type="text/css">
        body {
            padding: 10px;
        }
    </style>

</head>
<body>
<form action="" class="layui-form layui-form-pane">
    <input type="hidden" id="pkDepartmentId" name="pkDepartmentId">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">部门名称</label>
            <div class="layui-input-inline">
            <input autocomplete="off" maxlength="64" class="layui-input" lay-reqtext="人员名是必填项，岂能为空？" lay-verify="required"
                   name="departmentName" id="departmentName" placeholder="请输入(必填)" type="text">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">部门领导</label>
            <div class="layui-input-inline">
                <select  name="personnelId" id="personnelId">
                    <option selected="" value="">部门领导</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">上级类型</label>
            <div class="layui-input-inline">
                <select  name="superiorType" id="superiorType" lay-filter="superiorType">
                    <option value="0" selected="">公司</option>
                    <option value="1" >部门</option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">所属</label>
        <div class="layui-input-block" id="department1" >
            <select name="departmentSuperior" id="departmentSuperior" >
                <option selected="" value="" >所属公司</option>
            </select>
        </div>
        <div class="layui-input-block" id="department2" style="display: none">
            <select  name="departmentSuperior" id="departmentSuperior2">
                <option selected="" value="">所属部门</option>
            </select>
        </div>
    </div>

        <div class="layui-form-item">
            <div class="layui-inline" >
                <label class="layui-form-label">所属领导</label>
                <div class="layui-input-inline" >
                    <select name="personnelSuperior" id="personnelSuperior">
                        <option selected="" value="">上级领导</option>
                    </select>
                </div>
            </div>
        <div class="layui-inline">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-inline" >
                <input autocomplete="off" class="layui-input"  name="departmentEmil"  maxlength="16"
                       id="departmentEmil" placeholder="请输入..." type="text">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">关键字</label>
            <div class="layui-input-inline" >
                <input autocomplete="off" class="layui-input"  name="keyword" id="keyword" maxlength="16"
                       placeholder="请输入" type="text">
            </div>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" name="remark" id="remark" placeholder="请输入备注" maxlength="256"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn site-demo-active" id="sub" lay-filter="de1" lay-submit="" type="submit">立即提交</button>
        </div>
    </div>
</form>
<script>
    var form=null;
    layui.use(['form', 'layedit', 'laydate', 'layer'], function () {
         form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;



        form.on('select(superiorType)', function(data){
            //判断是否1为部门或者0公司
            if(data.value == 0){
                $("#department2").hide();
                $("#department1").show();
                form.render('select');
            }else{
                $("#department2").show();
                $("#department1").hide();
                form.render('select');//select是固定写法 不是选择器
            }
        });

            //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听提交
        form.on('submit(de1)', function (data) {
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            if($("#superiorType").val()==="0"){
                data.field.departmentSuperior=$("#departmentSuperior").val();
            }else{
                data.field.departmentSuperior=$("#departmentSuperior2").val();
            }
            $.ajax({
                url: getRootPath() +"OaDepartmentAction/addOrUpdateDepartment"
                , contentType: "application/json;charset=UTF-8"
                , data: JSON.stringify(data.field)
                , headers: {token: token}
                , type: "POST",
                success: res => {
                    if (res.code === 1) {
                        if (res.data.code === 1) {
                            window.parent.location.reload();//刷新父页面
                            const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.layer.close(index);
                        } else if (res.data.code === 500) {
                            error('500');
                        } else {
                            layer.msg("新增失败");
                        }
                    } else {
                        layer.msg(404);
                    }
                },
                error: res => {
                    console.log('9999');
                }
            });
            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
        });

        loadPerson();
        loadType();
        loaddepartment();
        //修改操作
        var ide = getQueryString("pkDepartmentId");
        if (ide) {
            findById(ide);//修改页面的赋值
        }else{
            $("#department2").hide();
        }
        form.render('select');//select是固定写法 不是选择器
        //重新渲染表单函数
        function renderForm() {
            layui.use('form', function () {
                var form = layui.form(); //高版本建议把括号去掉，有的低版本，需要加()
            });
        }

        form.render();
    });

    function findById(ide) {
            let url = getRootPath() + "OaDepartmentAction/selectById/" + ide;
            let data=getAjaxById(url);
            $("#pkDepartmentId").val(data.data.pkDepartmentId);
            $("#departmentName").val(data.data.departmentName);
            $("#departmentEmil").val(data.data.departmentEmil);
            $("#superiorType").val(data.data.superiorType);
            $("#remark").val(data.data.remark);
            $("#keyword").val(data.data.keyword);
            $("#departmentCode").val(data.data.departmentCode);
            $("#personnelId").val(data.data.personnelId);
            $("#personnelSuperior").val(data.data.personnelSuperior);
            if(data.data.superiorType === "0"){
                $("#departmentSuperior").val(data.data.departmentSuperior);
                $("#department1").show();
                $("#department2").hide();
            }else{
                $("#departmentSuperior2").val(data.data.departmentSuperior);
                $("#department2").show();
                $("#department1").hide();
            }

            form.render("select")

    }
    //加载部门下拉框
    function loadType(){
        var url=getRootPath()+"OaCompanyAction/companyFindByAll";
        var data=getAjax(url,null);
        $.each(data.data,function(index,item){
            $("#departmentSuperior").append("<option value='"+item.pkCompanyId+"'>"+item.companyName+"</option>");
        });
    }
    function loadPerson(){
        var url=getRootPath()+"OaPersonnelAction/personnelFindByAll";
        var data=getAjax(url,null);
        $.each(data.data,function(index,item){
            $("#personnelSuperior").append("<option value='"+item.pkPersonnelId+"'>"+item.personnelName+"</option>");
            $("#personnelId").append("<option value='"+item.pkPersonnelId+"'>"+item.personnelName+"</option>");
        });
    }
    function loaddepartment(){
        var url=getRootPath()+"OaDepartmentAction/departmentfindByAll";
        var data=getAjax(url,null);
        $.each(data.data,function(index,item){
            console.log(item);
            $("#departmentSuperior2").append("<option value='"+item.pkDepartmentId+"'>"+item.departmentName+"</option>");
        });
    }
</script>
</body>
</html>